package com.toidep.fragment.mains;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.toidep.R;
import com.toidep.activities.MainActivity;
import com.toidep.api.ApiController;
import com.toidep.api.ParseJSON;
import com.toidep.base.BaseFragment;
import com.toidep.config.ConfigUtility;
import com.toidep.config.Constants;
import com.toidep.entities.ArticleEntity;
import com.toidep.fragment.others.NewsDetailFragment;
import com.toidep.info.NewsDetailHolder;
import com.toidep.interfaces.ResponseListener;

public class HomeFragment extends BaseFragment implements OnClickListener {

	public boolean isRootFragment = true;
	
	private MainActivity mainActivity;
	
	private View view;
	
	private RelativeLayout layoutCatModel, layoutCatVideo, layoutCatAlbum, layoutIntro, layoutNews;
	private ImageView imageFeaturedNews;
	private TextView textFeaturedNews,textTitle,txtIntroduction, textNew1, textNew2, textNew3, textNew4;
	private LinearLayout layoutFeaturedNew, layoutNew1, layoutNew2, layoutNew3, layoutNew4;
	
	private ArrayList<ArticleEntity> listFeaturedNews;
	
	/**
	 * Set lai cac base value cho fragment: cac item cua thanh header, currentCategory cua menuSideBar
	 */
	public void setBaseValues() {
		ApiController.getIntroduction(getActivity(), new ResponseListener() {
			
			@Override
			public void processResponse(int inintstall) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void processResponse(String response) {
				// TODO Auto-generated method stub
				txtIntroduction.setText(response);
			}
		});
		mainActivity.currentCategory = Constants.CATEGORY_HOME;
		mainActivity.mainFragment.setTittle(mainActivity.getString(R.string.label_category_home));
		mainActivity.mainFragment.showButtonSlide(true);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_home, container, false);

		mainActivity = (MainActivity) getActivity();

		initComponents();
		
		getListNews();

		setBaseValues();
		return view;
	}

	private void initComponents() {
		
		layoutCatModel = (RelativeLayout) view.findViewById(R.id.layoutCategoryModel);
		layoutCatModel.setOnClickListener(this);
		layoutCatVideo = (RelativeLayout) view.findViewById(R.id.layoutCategoryVideo);
		layoutCatVideo.setOnClickListener(this);
		layoutCatAlbum = (RelativeLayout) view.findViewById(R.id.layoutCategoryAlbum);
		layoutCatAlbum.setOnClickListener(this);
		
		imageFeaturedNews = (ImageView) view.findViewById(R.id.imageFeaturedNewsHome);
		textFeaturedNews = (TextView) view.findViewById(R.id.textviewFeaturedNewsHomeDescription);
		textTitle = (TextView) view.findViewById(R.id.textviewTitle);
		layoutFeaturedNew = (LinearLayout) view.findViewById(R.id.layoutFeaturedNew);
		layoutFeaturedNew.setOnClickListener(this);
		

		layoutIntro = (RelativeLayout) view.findViewById(R.id.layoutIntro);
		layoutIntro.setOnClickListener(this);
		
		layoutNews = (RelativeLayout) view.findViewById(R.id.layoutNews);
		layoutNews.setOnClickListener(this);
		
		layoutNew1 = (LinearLayout) view.findViewById(R.id.layoutNew1);
		layoutNew1.setOnClickListener(this);
		textNew1 = (TextView) view.findViewById(R.id.textNew1);
		layoutNew2 = (LinearLayout) view.findViewById(R.id.layoutNew2);
		layoutNew2.setOnClickListener(this);
		textNew2 = (TextView) view.findViewById(R.id.textNew2);
		layoutNew3 = (LinearLayout) view.findViewById(R.id.layoutNew3);
		layoutNew3.setOnClickListener(this);
		textNew3 = (TextView) view.findViewById(R.id.textNew3);
		layoutNew4 = (LinearLayout) view.findViewById(R.id.layoutNew4);
		layoutNew4.setOnClickListener(this);
		textNew4 = (TextView) view.findViewById(R.id.textNew4);
		
		txtIntroduction = (TextView) view.findViewById(R.id.txtIntroduction);
		
		listFeaturedNews = new ArrayList<ArticleEntity>();
		
	}
	
	private void getListNews() {
		
		try {
			((MainActivity) getActivity()).showProgressDialog();
		} catch (Exception e) {
		}
		
		ApiController.getHomeDetail(getActivity(), new ResponseListener() {
			
			@Override
			public void processResponse(int inintstall) {
				try {
					((MainActivity) getActivity()).closeProgressDialog();
				} catch (Exception e) {
				}
			}
			
			@Override
			public void processResponse(String response) {
				try {
					((MainActivity) getActivity()).closeProgressDialog();
				} catch (Exception e) {
				}
				if (response != null) {
//					Log.e(TAG, response);
					try {
						listFeaturedNews = ParseJSON.getAllNewsObjectFromResponse(response);
						int size = listFeaturedNews.size();
						if (size > 0)
							showFeaturedNews();
					} catch (Exception e) { }
				}
			}
		});
		
	}
	
	private void showFeaturedNews() {
		
		ArticleEntity newsItem = listFeaturedNews.get(0);
		
		int widthImage = ConfigUtility.size_half_width;
		int heightImage = (int) (widthImage*ConfigUtility.scale_size_horizontal_thumbs_item);
		
		int widthImageView = (int) (ConfigUtility.scrWidth*ConfigUtility.scale_thumbs_news_width);
		int heightImageView = (int) (widthImageView*ConfigUtility.scale_size_horizontal_thumbs_item);
		
		((MainActivity) getActivity()).loadImage(imageFeaturedNews, newsItem.getImageUrl(), widthImage, heightImage, widthImageView, heightImageView);
		textFeaturedNews.setText(newsItem.getIntroText());
		textTitle.setText(newsItem.getTitle());
		
		try {
			ArticleEntity item = listFeaturedNews.get(1);
			textNew1.setText(item.getTitle());
			layoutNew1.setVisibility(View.VISIBLE);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			ArticleEntity item = listFeaturedNews.get(2);
			textNew2.setText(item.getTitle());
			layoutNew2.setVisibility(View.VISIBLE);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			ArticleEntity item = listFeaturedNews.get(3);
			textNew3.setText(item.getTitle());
			layoutNew3.setVisibility(View.VISIBLE);
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
			ArticleEntity item = listFeaturedNews.get(4);
			textNew4.setText(item.getTitle());
			layoutNew4.setVisibility(View.VISIBLE);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public static HomeFragment newInstance() {
		HomeFragment myFragment = new HomeFragment();

		myFragment.setRootFragment(true);
		myFragment.setSupperRootFragment(true);
		
		return myFragment;
	}

	@Override
	public void onClick(View v) {
		if (v == layoutIntro) {
			mainActivity.doClick(R.id.layoutButtonIntro);
		} else if (v == layoutNews) {
			onNewItemClick(0);
		} else if (v == layoutFeaturedNew) {
			onNewItemClick(0);
		} else if (v == layoutNew1 ) {
			onNewItemClick(1);
		} else if (v == layoutNew2 ) {
			onNewItemClick(2);
		} else if (v == layoutNew3 ) {
			onNewItemClick(3);
		} else if (v == layoutNew4 ) {
			onNewItemClick(4);
		} else {
			mainActivity.doClick(v.getId());
		}
	}
	
	public void onNewItemClick(int position) {
		try {
			NewsDetailHolder.setListNewDetail(listFeaturedNews);
			NewsDetailHolder.setCurrentPosition(position);
			
			mainActivity.mainFragment.openFragmentScreen(NewsDetailFragment.newInstance(0), null);	// Can sua lai newType
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
}
